<?php
class Aoperations {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__dofollowed();
				break;
			case 2:
				self::__dosubscribe();
				break;
			case 3:
				self::__domorefollowed();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//关注主播3-1-1
	private static function __dofollowed() {
		$fansid  = intval(sview::$ctrl['userid']);
		$status  = intval(sview::$ctrl['status']);
		$userid  = acl::getinst('vedio')->user['id'];
		$studio  = model::load('live/studio')->get_item(array('userid'=>$fansid, 'status'=>1));
		if (($status == 1 && $studio) || $status == 0) {
			api_stats::sfans($userid, $fansid, $status, $studio['id']);
		}
		sview::$view->display();
	}
	
	//订阅直播3-1-2
	private static function __dosubscribe() {
		$studioid= intval(sview::$ctrl['studioid']);
		$status  = intval(sview::$ctrl['status']);
		$userid  = acl::getinst('vedio')->user['id'];
		$studio  = model::load('live/studio')->get_one($studioid);
		//判断直播间是否存在
		if ($studio) {
			switch ($status) {
				case 1:
					$where = array('userid'=>$userid, 'studioid'=>$studio['id'], 'airtime'=>$studio['airtime']);
					if (0 < model::load('live/subscibe')->get_total($where)) {
						sview::$view->assign_error(80004, lang::text(80004));
					}
					$fields = array('userid'=>$userid, 'studioid'=>$studio['id'],
							'airtime'=>$studio['airtime'], 'status'=>0, 'stime'=>config::$nowtime);
					model::load('live/subscibe')->new_one($fields);
					break;
				default:
					model::load('live/subscibe')->get_orm()->where('userid', $userid)
						->where('studioid', $studio['id'])->delete();
					model::load('live/subscibe')->save_cache_ver();
			}
		}
		sview::$view->display();
	}
	
	//一键关注主播3-1-3
	private static function __domorefollowed() {
		$userid  = acl::getinst('vedio')->user['id'];
		$data    = api_stats::recommend();
		if (!empty($data)) {
			foreach ($data as $item) {
				if ($userid == $item['userid']) {
					continue;
				}
				api_stats::sfans($userid, $item['userid'], 1, $item['studioid']);
			}
			$field = array('isfocus'=>2);
			model::load('live/userinfo')->set_devision($userid)->save($userid, $field);
		}
		sview::$view->display();
	}
}